#include "LeVideoEditorUI.h"
//XVideoUI.cpp
#include<QFileDialog>
#include<string>
#include<qmessagebox>
#include"XVideoThread.h"


LeVideoEditorUI::LeVideoEditorUI(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	//setWindowFlags(Qt::FramelessWindowHint);
	qRegisterMetaType<Mat>("Mat");
	QObject::connect(
		XVideoThread::instance(),
		SIGNAL(ViewImage1(Mat)),
		ui.src1,
		SLOT(setImage(Mat))
		);
	startTimer(40);
}

//该事件需要在头文件中定义槽函数
//在ui编辑器中拖动事件pushbutton-》当前widget  
//接受函数可以在UI编辑器中指定
void LeVideoEditorUI::open()
{
	QString name = QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("选择视频文件"));//防止乱码
	if (name.isEmpty())
	{
		return;
	}
    std::string filename=name.toLocal8Bit().data();
	if (!XVideoThread::instance()->openVideo(filename)) {
		std::cout << "open faild:" <<filename<< std::endl;
		QMessageBox::information(this, "error", name + " open failed!");
		return;
	}
	std::cout << "open success:"<<filename << std::endl;
}

void LeVideoEditorUI::timerEvent(QTimerEvent * e)
{
	if (isPressed)
	{
		return;
	}
	double pos = XVideoThread::instance()->getPos();
	ui.playSlider->setValue(pos*1000);

}

void LeVideoEditorUI::slidePress()
{
	isPressed = true;
}

void LeVideoEditorUI::slideRelease()
{
	isPressed = false;
}

//UI控件通过槽传递过来的 滑动当前数值0-1000
void LeVideoEditorUI::setPos(int pos)
{
	//pos /1000 获取滑动位置（0-1）*1000   
	XVideoThread::instance()->seek((double)pos/1000.0);
}
